Conversation
jknack
commented
Dec 8, 2024
- simplify custom ConstraintValidatorFactory based on DI as well as manually created factories
- integrates Validator factory into hibernate
|
@kliushnichenko please check (ignore the Jetty change) |
kliushnichenko
left a comment
There was a problem hiding this comment.
Cool! Added my 2 cents to it
| In this case you need to implement a custom `ConstraintValidator` that will rely on your DI framework | ||
| instantiating your custom `ConstraintValidator` | ||
|
|
||
| 1) Implement custom `ConstraintValidatorFactory`: |
There was a problem hiding this comment.
ConstraintValidatorFactory, guess we can delete the whole line, it's one step only now
|
|
||
| [source, java] | ||
| ---- | ||
| public class MyCustomValidator implements ConstraintValidator<MyCustomAnnotation, Bean> { |
There was a problem hiding this comment.
for avaje it's important to add @Singleton annotation, otherwise, the instantiation will fail, and the error message is not clear - it hides the real reason related to DI.
| private String title = "Validation failed"; | ||
| private boolean disableDefaultViolationHandler = false; | ||
| private boolean logException = false; | ||
| private List<ConstraintValidatorFactory> factories; |
There was a problem hiding this comment.
just curious, do you have real use cases in mind where you need multiple factories?
There was a problem hiding this comment.
Isn't need it if we rely on DI. So this is for people who doesn't like DI (reflective or not), so they can just do:
getInstance(Class type) {
if (type == Validator1) {..}
else if (type== Validator2) {...}
else return null
}
| @Override | ||
| public <T extends ConstraintValidator<?, ?>> T getInstance(Class<T> key) { | ||
| try { | ||
| return registry.require(key); |
There was a problem hiding this comment.
should work for Avaje/Guice, but not for Dagger. A note in docs that, for the Dagger, the factory needs to be implemented manually, would be helpful.
There was a problem hiding this comment.
not sure if possible to implement a registry for Dagger, will add a note.
…tory - simplify custom ConstraintValidatorFactory based on DI as well as manually created factories - integrates Validator factory into hibernate - fix #3595